package com.servlet;

import com.common.Constant;
import com.common.SendMail;
import com.common.pcBean;
import com.service.pcFixService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by ztc on 16-1-12.下午3:09
 */
public class EmailWe extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies=request.getCookies();
        if(cookies!=null&&cookies.length==2) {
            String savePath = this.getServletContext().getRealPath("/img/upload");
            File file = new File(savePath);
            //判断上传文件的保存目录是否存在
            if (!file.exists() && !file.isDirectory()) {
                file.mkdir();
            }
            //判断是不是带MultipartContent的请求
            if(!ServletFileUpload.isMultipartContent(request)){
                System.out.println("isMultipartContent");
                return;
            }

            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            //解决文件名乱码
            upload.setHeaderEncoding("UTF-8");
            //设置文件最大大小
            upload.setFileSizeMax(1024 * 1024 * 4);

            try {
                Map<String,List<FileItem>> params=upload.parseParameterMap(request);
                //获取请求的参数
                String xh=params.get("xh").get(0).getString("UTF-8");
                String qq=params.get("qq").get(0).getString("UTF-8");
                String des=params.get("des").get(0).getString("UTF-8");
                String weixin=params.get("weixin").get(0).getString("UTF-8");
                String phone=params.get("phone").get(0).getString("UTF-8");
                String name=params.get("name").get(0).getString("UTF-8");
                String email=params.get("email").get(0).getString("UTF-8");
                FileItem pic=params.get("pic").get(0);
                System.out.println(pic);
                //图片保存
                if(pic.getSize()!=0) {
                    String filename = pic.getName();
                    filename = filename.substring(filename.lastIndexOf("\\") + 1);
                    savePath = savePath + "/" + filename;
                    InputStream inputStream = pic.getInputStream();
                    FileOutputStream outputStream = new FileOutputStream(savePath);
                    byte[] buffer = new byte[1024];
                    int count;
                    while ((count = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, count);
                    }
                    inputStream.close();
                    outputStream.close();
                    pic.delete();
                }
                //邮件发送
                new SendMail(Constant.mailRcpt,Constant.mailSubject,
                        "姓名:"+name+"<br>"
                                +"学号:"+xh+"<br>"
                                +"电话:"+phone+"<br>"
                                +"Q Q:"+qq+"<br>"
                                +"微信:"+weixin+"<br>"
                                +"邮箱:"+email+"<br>"
                                +"描述:"+des+"<br>",savePath).start();
                //保存到数据库
                System.out.println(xh+" "+name+" "+phone+" "+qq+" "+weixin+" "+email+" "+" "+des+" "+savePath);
                new pcFixService().insertPc(new pcBean(xh,name,phone,qq,weixin,email,des,savePath));
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
            request.getRequestDispatcher("/pcFixList.jsp?curPage=1&pageSize=10").forward(request,response);
        }else{
            response.sendRedirect("pcFix.jsp");
        }
    }
}
